{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "de83e566",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "80a14615",
   "metadata": {},
   "outputs": [],
   "source": [
    "# loading data\n",
    "ad_df = pd.read_csv('./data/ad.csv')\n",
    "train_df = pd.read_csv('./data/train.csv')\n",
    "test_df = pd.read_csv('./data/test.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "83435729",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>creativeID</th>\n",
       "      <th>adID</th>\n",
       "      <th>camgaignID</th>\n",
       "      <th>advertiserID</th>\n",
       "      <th>appID</th>\n",
       "      <th>appPlatform</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>4079</td>\n",
       "      <td>2318</td>\n",
       "      <td>147</td>\n",
       "      <td>80</td>\n",
       "      <td>14</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>4565</td>\n",
       "      <td>3593</td>\n",
       "      <td>632</td>\n",
       "      <td>3</td>\n",
       "      <td>465</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3170</td>\n",
       "      <td>1593</td>\n",
       "      <td>205</td>\n",
       "      <td>54</td>\n",
       "      <td>389</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>6566</td>\n",
       "      <td>2390</td>\n",
       "      <td>205</td>\n",
       "      <td>54</td>\n",
       "      <td>389</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5187</td>\n",
       "      <td>411</td>\n",
       "      <td>564</td>\n",
       "      <td>3</td>\n",
       "      <td>465</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   creativeID  adID  camgaignID  advertiserID  appID  appPlatform\n",
       "0        4079  2318         147            80     14            2\n",
       "1        4565  3593         632             3    465            1\n",
       "2        3170  1593         205            54    389            1\n",
       "3        6566  2390         205            54    389            1\n",
       "4        5187   411         564             3    465            1"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ad_df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "627b53bc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>label</th>\n",
       "      <th>clickTime</th>\n",
       "      <th>conversionTime</th>\n",
       "      <th>creativeID</th>\n",
       "      <th>userID</th>\n",
       "      <th>positionID</th>\n",
       "      <th>connectionType</th>\n",
       "      <th>telecomsOperator</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>170000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>3089</td>\n",
       "      <td>2798058</td>\n",
       "      <td>293</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>170000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1259</td>\n",
       "      <td>463234</td>\n",
       "      <td>6161</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>170000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>4465</td>\n",
       "      <td>1857485</td>\n",
       "      <td>7434</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "      <td>170000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1004</td>\n",
       "      <td>2038823</td>\n",
       "      <td>977</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>170000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1887</td>\n",
       "      <td>2015141</td>\n",
       "      <td>3688</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   label  clickTime  conversionTime  creativeID   userID  positionID  \\\n",
       "0      0     170000             NaN        3089  2798058         293   \n",
       "1      0     170000             NaN        1259   463234        6161   \n",
       "2      0     170000             NaN        4465  1857485        7434   \n",
       "3      0     170000             NaN        1004  2038823         977   \n",
       "4      0     170000             NaN        1887  2015141        3688   \n",
       "\n",
       "   connectionType  telecomsOperator  \n",
       "0               1                 1  \n",
       "1               1                 2  \n",
       "2               4                 1  \n",
       "3               1                 1  \n",
       "4               1                 1  "
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "57246c39",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3749528, 8)"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_df.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b3965304",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>label</th>\n",
       "      <th>clickTime</th>\n",
       "      <th>conversionTime</th>\n",
       "      <th>creativeID</th>\n",
       "      <th>userID</th>\n",
       "      <th>positionID</th>\n",
       "      <th>connectionType</th>\n",
       "      <th>telecomsOperator</th>\n",
       "      <th>adID</th>\n",
       "      <th>camgaignID</th>\n",
       "      <th>advertiserID</th>\n",
       "      <th>appID</th>\n",
       "      <th>appPlatform</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>170000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>3089</td>\n",
       "      <td>2798058</td>\n",
       "      <td>293</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1321</td>\n",
       "      <td>83</td>\n",
       "      <td>10</td>\n",
       "      <td>434</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>170000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1259</td>\n",
       "      <td>463234</td>\n",
       "      <td>6161</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1535</td>\n",
       "      <td>685</td>\n",
       "      <td>80</td>\n",
       "      <td>14</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>170000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>4465</td>\n",
       "      <td>1857485</td>\n",
       "      <td>7434</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>147</td>\n",
       "      <td>460</td>\n",
       "      <td>3</td>\n",
       "      <td>465</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "      <td>170000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1004</td>\n",
       "      <td>2038823</td>\n",
       "      <td>977</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>411</td>\n",
       "      <td>564</td>\n",
       "      <td>3</td>\n",
       "      <td>465</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>170000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1887</td>\n",
       "      <td>2015141</td>\n",
       "      <td>3688</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>369</td>\n",
       "      <td>144</td>\n",
       "      <td>84</td>\n",
       "      <td>360</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   label  clickTime  conversionTime  creativeID   userID  positionID  \\\n",
       "0      0     170000             NaN        3089  2798058         293   \n",
       "1      0     170000             NaN        1259   463234        6161   \n",
       "2      0     170000             NaN        4465  1857485        7434   \n",
       "3      0     170000             NaN        1004  2038823         977   \n",
       "4      0     170000             NaN        1887  2015141        3688   \n",
       "\n",
       "   connectionType  telecomsOperator  adID  camgaignID  advertiserID  appID  \\\n",
       "0               1                 1  1321          83            10    434   \n",
       "1               1                 2  1535         685            80     14   \n",
       "2               4                 1   147         460             3    465   \n",
       "3               1                 1   411         564             3    465   \n",
       "4               1                 1   369         144            84    360   \n",
       "\n",
       "   appPlatform  \n",
       "0            1  \n",
       "1            2  \n",
       "2            1  \n",
       "3            1  \n",
       "4            1  "
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train = pd.merge(train_df,ad_df,on='creativeID')\n",
    "train.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "7214076a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3749528, 13)"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0b1e924e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 统计 APP 转换率\n",
    "cvr = train.groupby('appID')['label'].agg(['mean'])\n",
    "cvr.columns = ['app_cvr']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "3168ec3f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>app_cvr</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>appID</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>0.002515</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>0.006042</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>68</th>\n",
       "      <td>0.000431</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75</th>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>83</th>\n",
       "      <td>0.106286</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        app_cvr\n",
       "appID          \n",
       "14     0.002515\n",
       "25     0.006042\n",
       "68     0.000431\n",
       "75     0.000000\n",
       "83     0.106286"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cvr.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cb9a915a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>instanceID</th>\n",
       "      <th>label</th>\n",
       "      <th>clickTime</th>\n",
       "      <th>creativeID</th>\n",
       "      <th>userID</th>\n",
       "      <th>positionID</th>\n",
       "      <th>connectionType</th>\n",
       "      <th>telecomsOperator</th>\n",
       "      <th>adID</th>\n",
       "      <th>camgaignID</th>\n",
       "      <th>advertiserID</th>\n",
       "      <th>appID</th>\n",
       "      <th>appPlatform</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>-1</td>\n",
       "      <td>310000</td>\n",
       "      <td>3745</td>\n",
       "      <td>1164848</td>\n",
       "      <td>3451</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1166</td>\n",
       "      <td>430</td>\n",
       "      <td>80</td>\n",
       "      <td>14</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>-1</td>\n",
       "      <td>310000</td>\n",
       "      <td>2284</td>\n",
       "      <td>2127247</td>\n",
       "      <td>1613</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1388</td>\n",
       "      <td>325</td>\n",
       "      <td>80</td>\n",
       "      <td>14</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>-1</td>\n",
       "      <td>310000</td>\n",
       "      <td>1456</td>\n",
       "      <td>2769125</td>\n",
       "      <td>5510</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>3379</td>\n",
       "      <td>411</td>\n",
       "      <td>3</td>\n",
       "      <td>465</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>-1</td>\n",
       "      <td>310000</td>\n",
       "      <td>4565</td>\n",
       "      <td>9762</td>\n",
       "      <td>4113</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>3593</td>\n",
       "      <td>632</td>\n",
       "      <td>3</td>\n",
       "      <td>465</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>-1</td>\n",
       "      <td>310000</td>\n",
       "      <td>49</td>\n",
       "      <td>2513636</td>\n",
       "      <td>3615</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1469</td>\n",
       "      <td>535</td>\n",
       "      <td>80</td>\n",
       "      <td>14</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   instanceID  label  clickTime  creativeID   userID  positionID  \\\n",
       "0           1     -1     310000        3745  1164848        3451   \n",
       "1           2     -1     310000        2284  2127247        1613   \n",
       "2           3     -1     310000        1456  2769125        5510   \n",
       "3           4     -1     310000        4565     9762        4113   \n",
       "4           5     -1     310000          49  2513636        3615   \n",
       "\n",
       "   connectionType  telecomsOperator  adID  camgaignID  advertiserID  appID  \\\n",
       "0               1                 3  1166         430            80     14   \n",
       "1               1                 3  1388         325            80     14   \n",
       "2               2                 1  3379         411             3    465   \n",
       "3               2                 3  3593         632             3    465   \n",
       "4               1                 3  1469         535            80     14   \n",
       "\n",
       "   appPlatform  \n",
       "0            2  \n",
       "1            2  \n",
       "2            1  \n",
       "3            1  \n",
       "4            2  "
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "test = pd.merge(test_df,ad_df,on='creativeID')\n",
    "test.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "674f304e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "instanceID          0\n",
       "label               0\n",
       "clickTime           0\n",
       "creativeID          0\n",
       "userID              0\n",
       "positionID          0\n",
       "connectionType      0\n",
       "telecomsOperator    0\n",
       "adID                0\n",
       "camgaignID          0\n",
       "advertiserID        0\n",
       "appID               0\n",
       "appPlatform         0\n",
       "dtype: int64"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test.isnull().sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3e0cda06",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>appID</th>\n",
       "      <th>app_cvr</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>14</td>\n",
       "      <td>0.002515</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>14</td>\n",
       "      <td>0.002515</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>465</td>\n",
       "      <td>0.020328</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>465</td>\n",
       "      <td>0.020328</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>14</td>\n",
       "      <td>0.002515</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>338484</th>\n",
       "      <td>465</td>\n",
       "      <td>0.020328</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>338485</th>\n",
       "      <td>391</td>\n",
       "      <td>0.009974</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>338486</th>\n",
       "      <td>328</td>\n",
       "      <td>0.016667</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>338487</th>\n",
       "      <td>465</td>\n",
       "      <td>0.020328</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>338488</th>\n",
       "      <td>465</td>\n",
       "      <td>0.020328</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>338489 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        appID   app_cvr\n",
       "0          14  0.002515\n",
       "1          14  0.002515\n",
       "2         465  0.020328\n",
       "3         465  0.020328\n",
       "4          14  0.002515\n",
       "...       ...       ...\n",
       "338484    465  0.020328\n",
       "338485    391  0.009974\n",
       "338486    328  0.016667\n",
       "338487    465  0.020328\n",
       "338488    465  0.020328\n",
       "\n",
       "[338489 rows x 2 columns]"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 填充测试集的app转换率\n",
    "pd.merge(test,cvr,on='appID',how='left').fillna(train['label'].mean())[['appID','app_cvr']]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2ad00cce",
   "metadata": {},
   "source": [
    "### 基于 AD + LR 的版本    Logistic Regression 逻辑回归"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "a7e50874",
   "metadata": {},
   "outputs": [],
   "source": [
    "# load model\n",
    "from sklearn.preprocessing import OneHotEncoder\n",
    "from sklearn.linear_model import LogisticRegression"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fee10871",
   "metadata": {},
   "outputs": [],
   "source": [
    "# loading data\n",
    "ad_df = pd.read_csv('./data/ad.csv')\n",
    "train_df = pd.read_csv('./data/train.csv')\n",
    "test_df = pd.read_csv('./data/test.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6917fd24",
   "metadata": {},
   "outputs": [],
   "source": [
    "# test + ad   train + ad\n",
    "test = pd.merge(test_df,ad_df,on='creativeID')\n",
    "train = pd.merge(train_df,ad_df,on='creativeID')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "be8c6380",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 0, 0, ..., 0, 0, 0], dtype=int64)"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_train = train['label'].values "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "59718ffd",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 设置要特征编码的列\n",
    "feats = ['appID','appPlatform']\n",
    "\n",
    "enc = OneHotEncoder()\n",
    "\n",
    "x_train = enc.fit_transform(train[feats])\n",
    "\n",
    "x_test = enc.transform(test[feats])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "60b07923",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.00250741, 0.00250741, 0.02029911, ..., 0.01671791, 0.02029911,\n",
       "       0.02029911])"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 建立模型\n",
    "\n",
    "import warnings \n",
    "warnings.filterwarnings('ignore')\n",
    "lr = LogisticRegression()\n",
    "\n",
    "lr.fit(x_train,y_train)\n",
    "\n",
    "proba_test = lr.predict_proba(x_test)[:,1]\n",
    "proba_test"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "data_ana_project_env",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
